<template>
  <div>
    <div>
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="规格名称">
          <el-input v-model="specName" placeholder="规格名称"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="list">查询</el-button>
          <el-button type="primary" @click="openAdd">添加</el-button>
          <el-button type="primary" @click="reversCheck">反选</el-button>
          <el-button type="danger" @click="delSpecByIds">批量删除</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-table
        :data="tableData"
        style="width: 100%"
        ref="multipleTable"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>

        <el-table-column label="规格ID" width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.id }}</span>
          </template>
        </el-table-column>

        <el-table-column label="规格名称" width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.specName }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="startPage"
        :page-sizes="[3, 5, 7, 10]"
        :page-size="endPage"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- ______________________________________________________________________________ -->
    <div>
      <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
        <el-form :model="specification">
          <el-form-item label="规格名称" :label-width="formLabelWidth">
            <el-input
              v-model="specification.specName"
              autocomplete="off"
            ></el-input>
          </el-form-item>

          <el-button type="primary" @click="addOption">新增规格选项</el-button>

          <!-- 放入表格 -->
          <el-table
            :data="this.specification.specificationOptionList"
            height="250"
            style="width: 100%"
          >
            <el-table-column prop="name" label="规格选项" width="180">
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.optionName"
                  placeholder="规格选项"
                  style="width: auto"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="name" label="排序" width="180">
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.orders"
                  placeholder="排序"
                  style="width: auto"
                ></el-input>
              </template>
            </el-table-column>

            <el-table-column prop="address" label="操作">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="danger"
                  @click="delOption(scope.$index, scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="saveOrUpdate">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import {
  listApi,
  addSpecificationApi,
  updateSpecificationApi,
  findByIdApi,
  delByIdApi,
  delByBath,
} from "@/api/specification";
export default {
  data() {
    return {
      ids: [],
      tableData: [],
      specName: "",
      startPage: 1,
      endPage: 3,
      total: 0,
      dialogFormVisible: false,
      specification: {
        id: null,
        specName: "",
        specificationOptionList: [],
      },
    };
  },
  created() {
    this.list();
  },
  methods: {
    list() {
      listApi(this.startPage, this.endPage, this.specName).then((result) => {
        this.tableData = result.data.data.list;
        this.total = result.data.data.total;
      });
    },
    handleEdit(index, row) {
      findByIdApi(row.id).then((result) => {
        this.specification = result.data.data;
        this.dialogFormVisible = true;
      });
    },
    addOption() {
      this.specification.specificationOptionList.push({});
    },
    delOption(index, row) {
      this.specification.specificationOptionList.splice(index, 1);
    },
    openAdd() {
      this.dialogFormVisible = true;
    },
    reversCheck() {
      this.tableData.forEach((row) => {
        this.$refs.multipleTable.toggleRowSelection(row);
      });
    },
    handleDelete(index, row) {
      delByIdApi(row.id).then((result) => {
        this.list();
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.endPage = val;
      this.list();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.startPage = val;
      this.list();
    },
    handleSelectionChange(val) {
      this.ids = [];
      if (val.length > 0) {
        val.forEach((row) => {
          this.ids.push(row.id);
        });
      } else {
        this.ids = [];
      }
    },
    delSpecByIds() {
      delByBath(this.ids);
      this.list();
    },
    saveOrUpdate() {
      if (this.specification.id == null) {
        addSpecificationApi(this.specification).then((result) => {
          this.dialogFormVisible = false;
          (this.specification.specName = ""),
            (this.specification.id = null),
            (this.specification.specificationOptionList = []),
            this.list();
        });
      } else {
        updateSpecificationApi(this.specification).then((result) => {
          this.dialogFormVisible = false;
          (this.specification.specName = ""),
            (this.specification.id = null),
            (this.specification.specificationOptionList = []),
            this.list();
        });
      }
    },
  },
};
</script>
